#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 100
typedef int ElemType;

typedef struct TreeNode
{
	ElemType weight;
	struct TreeNode *left;
	struct TreeNode *right;
}TreeNode;

typedef TreeNode* BiTree;

int idx = 0;
int weight[] = {100, 42, 15, -1, -1, 27, -1, -1, 58, 28, 13, 5, -1, -1, 8, -1, -1, 15, -1, -1, 30, -1, -1};

void createTree(BiTree *T)
{
	ElemType ch;
	ch = weight[idx++];

	if (ch == -1)
	{
		*T = NULL;
	}
	else
	{
		*T = (BiTree)malloc(sizeof(TreeNode));
		(*T)->weight = ch;
		createTree(&(*T)->left);
		createTree(&(*T)->right);
	}
}

//考研题目
int wpl(BiTree t){
  if(t==NULL) return 0;
  //试卷上无法写下足够篇幅的队列实现，可直接用数组模拟
  BiTree queue[MAXSIZE];
  int front=0,rear=0;
  int wpl=0,depth=0;

  queue[rear++]=t;

  while(rear!=front){
    int count=rear-front;
    while(count>0){
      BiTree cur=queue[front++];
      if(cur->left!=NULL){
        queue[rear++]=cur->left;
      }
      if(cur->right!=NULL){
        queue[rear++]=cur->right;
      }
      if(cur->left==NULL&&cur->right==NULL){
        wpl+=cur->weight*depth;
      }
      count--;
    }
    depth++;
  }
  return wpl;
}


int main(int argc, char const *argv[])
{
	
	BiTree T;
	createTree(&T);
	int w = wpl(T);
	printf("%d\n", w);

	return 0;
}



